package Action;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import Dao.roomdao;
import Dao.userdao;
import Entity.Room;
import Entity.User;
import mapper.Roommapper;
import mapper.Usermapper;

/**
 * Servlet implementation class LoginServlet
 */
@WebServlet("/LoginServlet")
public class LoginServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#service(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
        String Uid=req.getParameter("stuNum");
        String Pwd=req.getParameter("psw");
        User u= new User();
        u.setUid(Uid);
        u.setPwd(Pwd);
        //瀹炵幇鐧诲綍鍔熻兘鐨勬湇鍔″眰
        userdao l = new userdao();
        User user= null;
        user = l.Validate(u);
        if(user!=null)
        System.out.println(user.toString());
        if(user!=null){
    		Roommapper r =new roomdao();
    		List<Room> lr= r.Room_query_all();
    		req.setAttribute("lr", lr);
            HttpSession session = req.getSession();
            session.setAttribute("user",user);
            req.setAttribute("errorpsw",null);
            if(user.getUtype()==1)
                req.getRequestDispatcher("RoomServlet").forward(req,resp);
            if(user.getUtype()==2)
            	req.getRequestDispatcher("queryCourses").forward(req,resp);
            if(user.getUtype()==3)
            	resp.sendRedirect("SsearchServlet");
        }else{
            req.setAttribute("errorpsw","wrong psw!");
            req.getRequestDispatcher("login.jsp").forward(req,resp);
        }
	}

}
